let app = getApp();
Page({
    data: {
        currentTab: 1,
        priceRanges: ['免费', '¥50以下', '¥50 - 100', '¥100 - 500', '¥500 - 1000', '¥1000以上'],
        courseList: [],
        pageNo: 1,
        totalPageCount: 0,
        isLoading: false,
        courseTypeList: [],
        courseTypeId: null,
        courseTypeName: '精选',
        url: ''
    },
    getData() {
        this.setData({
            isLoading: true
        });
        wx.showLoading({
            title: '正在加载中'
        });
        const that = this;
        const url = app.globalData.URL;
        wx.request({
            method: 'get',
            url: url + 'course/findByPage',
            data: {
                byNo: this.data.currentTab,
                courseTypeId: this.data.courseTypeId,
                pageNum: this.data.pageNo,
                pageSize: 5
            },
            success(res) {
                console.log(res);
                that.setData({
                    courseList: res.data.data.list,
                    totalPageCount: res.data.data.pages
                });
            },
            complete() {
                wx.hideLoading();
                wx.stopPullDownRefresh();
                that.setData({
                    isLoading: false
                });
            }
        });
        wx.request({
            method: 'get',
            url: url + 'courseType/all',
            success(res) {
                console.log(res.data.data);
                that.setData({
                    courseTypeList: res.data.data
                });
            },
            complete() {
                wx.hideLoading();
                wx.stopPullDownRefresh();
                that.setData({
                    isLoading: false
                });
            }
        });
    },
    top1: function (e) {
        const courseTypeId = e.currentTarget.dataset.coursetypeid;
        console.log("jhbifub"+courseTypeId)
        const that = this;
        const url = app.globalData.URL;
        wx.request({
            method: 'GET',
            url: url + 'course/courseTypeId',
            data: {
                courseTypeId: courseTypeId
            },
            success(res) {
                console.log(res);
                if (res.data.code === 200) {
                    that.setData({
                        courseList: res.data.data.list
                    });
                } else {
                    wx.showToast({
                        title: '暂无课程',
                        icon: 'none'
                    });
                }
            },
        });
    },
    show1: function () {
        this.setData({
            currentTab: 1,
            sortBy:"combined"
        });
        this.fetchAndSortCourses();
    },
    show2: function () {
        this.setData({
            currentTab: 2,
            sortBy:"collectionNumber"
        });
        this.fetchAndSortCourses();
    },
    show3: function () {
        this.setData({
            currentTab: 3,
            sortBy:"learningTimes"
        });
        this.fetchAndSortCourses();
    },
    show4: function () {
        this.setData({
            currentTab: 4,
            sortBy:"priceprice"
        });
        this.fetchAndSortCourses();
    },
    fetchAndSortCourses: function () {
      const that = this;
      const url = app.globalData.URL;
      const { currentTab, courseTypeId, sortBy } = this.data;
  
      if (courseTypeId!= null) {
          console.log("有courseTypeId");
          wx.request({
              method: 'GET',
              url: url + 'course/courseTypeId',
              data: {
                  courseTypeId: courseTypeId,
                  sortBy: sortBy
              },
              success(res) {
                  console.log("gghggg" + courseTypeId);
                  if (res.data.code === 200) {
                      const sortedCourses = that.sortCourses(res.data.data.list, currentTab, sortBy);
                      that.setData({
                          courseList: sortedCourses
                      });
                  } else {
                      wx.showToast({
                          title: '暂无课程',
                          icon: 'none'
                      });
                  }
              },
              fail(err) {
                  wx.showToast({
                      title: '网络请求失败',
                      icon: 'none'
                  });
              },
              complete() {
                  wx.hideLoading();
                  that.setData({
                      isLoading: false
                  });
              }
          });
      } else {
          console.log("没有courseTypeId");
          wx.request({
              method: 'get',
              url: url + 'course/all',
              data: {
                  byNo: currentTab,
                  pageNum: this.data.pageNo,
                  pageSize: 5,
                  sortBy: sortBy
              },
              success(res) {        
                console.log(sortBy),
                console.log(res.data.data)
                  if (res.data.code === 200) {
                      that.setData({
                          courseList: res.data.data.list,
                          totalPageCount: res.data.data.pages
                      });
                  } else {
                      wx.showToast({
                          title: '暂无课程',
                          icon: 'none'
                      });
                  }
              },
              complete() {
                  wx.hideLoading();
                  that.setData({
                      isLoading: false
                  });
              }
          });
      }
  },
    onLoad(options) {
        this.setData({
            url: app.globalData.URL
        });
        this.getData();
    },
    onReady() {
    },
    onShow() {
    },
    onHide() {
    },
    onUnload() {
    },
    onPullDownRefresh() {
        this.setData({
            courseList: [],
            pageNo: 1,
            totalPageCount: 0,
            isLoading: false
        });
        this.getData();
    },
    onReachBottom() {
        if (this.data.isLoading) {
            return;
        }
        console.log('触发了触底事件');
        this.setData({
            pageNo: this.data.pageNo + 1
        });
        if (this.data.pageNo > this.data.totalPageCount) {
            wx.showToast({
                title: '没有更多数据了'
            });
            return;
        }
        this.getData();
    },
    onShareAppMessage() {
    }
});